import fs from '@ohos.file.fs';
import { CryptoManager } from '../CryptoManager';
import { ExtVsMimeDict } from '../MimeExtMap';
import File from '@system.file';

/**
 * FilePathGenerator
 * 只负责生成App需要使用文件的路径.
 */


export class FilePathGenerator {
  static rootDir?: string
  static mailsDir?: string
  static buffDir?: string
  static cacheDir?: string
  static dbDir?: string
  static logDir?: string

  static initClass(rootDir: string, cache: string) {
    FilePathGenerator.rootDir = rootDir;
    FilePathGenerator.cacheDir = cache;
    FilePathGenerator.mailsDir = `${FilePathGenerator.rootDir}/mails`
    FilePathGenerator.buffDir = `${FilePathGenerator.rootDir}/buffers`
    FilePathGenerator.dbDir = `${FilePathGenerator.rootDir}/dbs`
    FilePathGenerator.logDir = `${FilePathGenerator.rootDir}/logs`
  }


  /**
   * mailKey由mid和folderFullName组成的字符串hash得到.
   * 不同文件夹的mail可能会有相同的mid;
   * @param mid 邮件的mid
   * @param folderFullName 邮件的文件夹
   */
  static async getDirPathForAccountMail(account: string, folderFullName: string, mid: string): Promise<string> {
    let accountKey: string = await CryptoManager.md5String(account);
    let mailKey: string = await CryptoManager.md5String(mid + folderFullName)
    let dirPath = `${FilePathGenerator.mailsDir!}/${accountKey}/${mailKey}`
    return dirPath;
  }

  static async getDirPathForAccount(account: string) {
    let accountKey: string = await CryptoManager.md5String(account);
    let dirPath = `${FilePathGenerator.mailsDir!}/${accountKey}`
    return dirPath;
  }

  static getFileTitleFromName(name: string) {
    if (!name) {
      return "";
    }
    let index = name.indexOf(".");
    return index == -1 ? name : name.substring(0, index);
  }

  /**
   * 先从filename取后缀,没有的话根据fileType取,都没有,返回unknown作为后缀名.
   * @param name
   * @param fileType
   * @returns
   */
  static getFileExtFromNameAndType(name: string, fileType: string) {
    if (name) {
      let index = name.indexOf(".");
      if (index != -1) {
        return name.substring(index + 1);
      }
    }
    let ext = Object.entries(ExtVsMimeDict).find(item => item[1].endsWith(fileType));
    if (ext) {
      return ext[0];
    } else {
      return "unknown";
    }
  }

  static async getDirPathForImportedMail(url: string): Promise<string> {
    let mailKey: string = await CryptoManager.md5String(url)
    return `${FilePathGenerator.mailsDir!}}/imported/${mailKey}`
  }

  static async getEmlPathForAccountMail(account: string, folderFullName: string, mid: string): Promise<string> {
    let mailDir = await this.getDirPathForAccountMail(account, folderFullName, mid);
    return `${mailDir}/index.eml`
  }

  static async getHtmlPathForAccountMail(account: string, folderFullName: string, mid: string): Promise<string> {
    let mailDir = await this.getDirPathForAccountMail(account, folderFullName, mid);
    return `${mailDir}/index.html`
  }

  static async getTextPathForAccountMail(account: string, folderFullName: string, mid: string): Promise<string> {
    let mailDir = await this.getDirPathForAccountMail(account, folderFullName, mid);
    return `${mailDir}/index.txt`
  }

  static async getEmlPathForImportedMail(url: string): Promise<string> {
    let mailDir = await this.getDirPathForImportedMail(url);
    return `${mailDir}/index.eml`
  }

  static async getHtmlPathForImportedMail(url: string): Promise<string> {
    let mailDir = await this.getDirPathForImportedMail(url);
    return `${mailDir}/index.html`
  }

  static async getTextPathForImportedMail(url: string): Promise<string> {
    let mailDir = await this.getDirPathForImportedMail(url);
    return `${mailDir}/index.txt`
  }

  static getFileName(fileTitle: string, fileExtension: string): string {
    return `${fileTitle}.${fileExtension}`
  }

  static async getAttachmentPathForAccountMail(account: string, folderFullName: string, mid: string, isInline: boolean,
    fileTile: string, fileExtension: string, index: number): Promise<string> {
    let mailDir = await FilePathGenerator.getDirPathForAccountMail(account, folderFullName, mid);
    let attachPath =
      `${mailDir}/${isInline ? "inline" : "extern"}/${index}/${FilePathGenerator.getFileName(fileTile, fileExtension)}`
    return attachPath
  }

  static async getAttachmentPathForImportedMail(url: string, isInline: boolean, fileTile: string, fileExtension: string,
    index: number): Promise<string> {
    let mailDir = await FilePathGenerator.getDirPathForImportedMail(url);
    let attachPath =
      `${mailDir}/${isInline ? "inline" : "extern"}/${index}/${FilePathGenerator.getFileName(fileTile, fileExtension)}`
    return attachPath;
  }

  static getFileContentType(fileName: string): string | undefined {
    let ext = fileName.split(".").pop();
    return ext ? ExtVsMimeDict[ext] : undefined;
  }

  static async getAttachmentDirPath(account: string, folderFullName: string, mid: string, isInline: boolean) {
    let mailDir = await FilePathGenerator.getDirPathForAccountMail(account, folderFullName, mid);
    return `${mailDir}/${isInline ? "inline" : "extern"}`
  }
}

